package cn.iocoder.yudao.module.erp.controller.admin.business.vip;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.vip.vo.VipPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.vip.vo.VipRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.vip.vo.VipSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.vip.VipDO;
import cn.iocoder.yudao.module.erp.service.business.vip.VipService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP客户级别")
@RestController
@RequestMapping("/erp/vip")
@Validated
public class VipController {

    @Resource
    private VipService vipService;

    @PostMapping("/create")
    @Operation(summary = "创建客户级别")
    @PreAuthorize("@ss.hasPermission('erp:vip:create')")
    public CommonResult<Long> createVip(@Valid @RequestBody VipSaveReqVO createReqVO) {
        return success(vipService.createVip(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户级别")
    @PreAuthorize("@ss.hasPermission('erp:vip:update')")
    public CommonResult<Boolean> updateVip(@Valid @RequestBody VipSaveReqVO updateReqVO) {
        vipService.updateVip(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户级别")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:vip:delete')")
    public CommonResult<Boolean> deleteVip(@RequestParam("id") Long id) {
        vipService.deleteVip(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户级别")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:vip:query')")
    public CommonResult<VipRespVO> getVip(@RequestParam("id") Long id) {
        VipDO vip = vipService.getVip(id);
        return success(BeanUtils.toBean(vip, VipRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户级别分页")
    @PreAuthorize("@ss.hasPermission('erp:vip:query')")
    public CommonResult<PageResult<VipRespVO>> getVipPage(@Valid VipPageReqVO pageReqVO) {
        PageResult<VipDO> pageResult = vipService.getVipPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VipRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户级别 Excel")
    @PreAuthorize("@ss.hasPermission('erp:vip:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVipExcel(@Valid VipPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VipDO> list = vipService.getVipPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户级别.xls", "数据", VipRespVO.class,
                        BeanUtils.toBean(list, VipRespVO.class));
    }

}